package com.lagou.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author sxnan
 * @className MyLocaleResovler
 * @description 国际化接口自定义实现
 * @date 2020/7/9
 */
@Configuration
public class MyLocaleResovler implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        Locale locale = null;
        if (!StringUtils.isEmpty(l)){//页面传入了参数，根据页面的参数改变语言环境
            String[] s = l.split("_");
            locale= new Locale(s[0],s[1]);
        }else {
            String header = httpServletRequest.getHeader("Accept-Language");
            String[] s = header.split(",");
            String[] s1 = s[0].split("-");
            locale= new Locale(s1[0],s1[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResovler();
    }
}
